<?php

//tailvh@gmail.com
//require forms file
require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "Multimediafile.php";

//require objects file
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "Multimediafile.php";

//require models file
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Multimediafiles.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Categorys.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Users.php";

class Admin_MultimediafileController extends Zend_Controller_Action {
    
    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $modelMultimediafile;
    private $modelCategory;
    private $modelUser;
    
    /////////////////////
    // O V E R I D E S //
    ///////////////////// 
    public function init() {

        /* Initialize action controller here */
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        $this->modelMultimediafile = new End_Models_Multimediafile();
        $this->modelUser = new End_Models_Users();
        $this->modelCategory = new End_Models_Categorys();
        //OTHER 
        $this->currentUser = $this->getInforUser();

        //VIEW 
        $this->view->currentUser = $this->currentUser;
        /* Initialize action controller here */
    }

     ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }
    
    ////////////////////
    // A C T I O N S  //
    //////////////////// 
    public function addAction() {
        //Create form enter info for new Multimediafile
        $form = new Application_Form_Multimediafile();
        $arrName = $this->modelCategory->_list();
        //Set label for the button
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');
        $modified_date = date('Y-m-d H:i:s');
        //Set client view Multimediafile form  = $form
        $this->view->form = $form;
        $form->category_id->AddMultiOptions($arrName);

        //If can get client request 
        if ($this->getRequest()->isPost()) {
            //cancel button     
            $cancel = $this->getRequest()->getPost('cancel', '0');

            if ($cancel) {
                $this->_helper->redirector('index');
            }

            //Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {   //If data is valid 
                //Get data one by one
                $category_id = (int) $form->getValue('category_id');
                $title = $form->getValue('title');
                $slug = $form->getValue('slug');
                $description = $form->getValue('description');
                $content = $form->getValue('content');
                $num_views = 0;
                $created_date = $modified_date;
                $created_user = '';
                $created_user_name = '';
                $allow_comment = (int) $form->getValue('allow_comment');
                $ordering = 0;
                $num_comments = 0;
                $url = '';
                $html_code = '';
                $is_active = '';
                $file_type = $form->getValue('file_type');
                $file_entity = $form->entityfile->receive();
                
                //Create new  Multimediafile object from datas
                $Multimediafile = new Multimediafile(null, $category_id, $title, $slug,
                                $description, $content,
                                $form->entityfile->getFileName(null, 0)/*image_thumpnail*/, $num_views,
                                $created_date, $created_user,
                                $created_user_name, $allow_comment,
                                $ordering, $num_comments,
                                $url, $html_code,
                                $is_active, $file_type);


                //Use insert function from  Multimediafile model
                $this->modelMultimediafile->_insert($Multimediafile);

                //Move to index Multimediafile
                $this->_helper->redirector('index');
            } else {
                //Return form
                $form->populate($formData);
            }
        }
    }

    public function deleteAction() {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') { 
                $file_id = (int) $this->_getParam('file_id');
                $this->modelMultimediafile->_delete($file_id);
            }
            $this->_helper->redirector('index');
        } else {
            $file_id = $this->_getParam('file_id', 0);
            $this->view->Multimediafiles = Utility_Converter::objectToArray($this->modelMultimediafile->_find($file_id));
        }
    }

    public function editAction() {
        $file_id = $this->_getParam('file_id');
        $arrName = $this->modelCategory->_list();
        $form = new Application_Form_Multimediafile();
        $form->category_id->AddMultiOptions($arrName);
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');
        $modified_date = date('Y-m-d H:i:s');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', 0);
            if ($cancel) {
                $this->_helper->redirector('index');
            }

            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $category_id = (int) $form->getValue('category_id');
                $title = $form->getValue('title');
                $slug = $form->getValue('slug');
                $description = $form->getValue('description');
                $content = $form->getValue('content');
                $image_thumbnail = '';
                $num_views = 0;
                $created_date = $modified_date;
                $created_user = '';
                $created_user_name = '';
                $allow_comment = (int) $form->getValue('allow_comment');
                $ordering = 0;
                $num_comments = 0;
                $url = '';
                $html_code = '';
                $is_active = '';
                $file_type = $form->getValue('file_type');
                //Create new  Multimediafile object from datas
                $Multimediafile = new Multimediafile($file_id, $category_id, $title, $slug,
                                $description, $content,
                                $image_thumbnail, $num_views,
                                $created_date, $created_user,
                                $created_user_name, $allow_comment,
                                $ordering, $num_comments,
                                $url, $html_code,
                                $is_active, $file_type);

                $this->modelMultimediafile->_update($Multimediafile);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $file_id = $this->_getParam('file_id', 0);
            if ($file_id > 0) {
                $form->populate(Utility_Converter::objectToArray($this->modelMultimediafile->_find($file_id)));
            }
        }
    }

    public function indexAction() {
        $Multimediafile = $this->modelMultimediafile->fetchAll();
        $this->view->Multimediafiles = $Multimediafile;
    }

}

